package com.yugao.fintech.draper.quartz.handler;

import com.yugao.fintech.draper.quartz.constants.ScheduleConstants;
import com.yugao.fintech.draper.quartz.task.ScheduledJob;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.quartz.JobExecutionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.Assert;

@RequiredArgsConstructor
public class ScheduleJobExecuteHandler extends QuartzJobBean {

	private final ApplicationContext applicationContext;

	private String jobBeanName;

	private String parameter;

	@Override
	protected void executeInternal(JobExecutionContext context) {
		jobBeanName = context.getMergedJobDataMap().getString(ScheduleConstants.JOB_BEAN_NAME);
		parameter = context.getMergedJobDataMap().getString(ScheduleConstants.PARAMETER);
		this.doExecute();
	}

	@SneakyThrows
	private void doExecute() {
		ScheduledJob job = applicationContext.getBean(jobBeanName, ScheduledJob.class);
		Assert.notNull(job, "job不存在！");
		job.execute(parameter);
	}
}
